<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\School;
use Encore\Admin\Controllers\ModelForm;

class SchoolSettingController extends Controller
{
    use ModelForm;

    private $header = '学校配置';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(School $school)
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit(School $school, $id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create(School $school)
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(School::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->column('name', '名称')->editable();
            $grid->column('detail', '描述')->limit(20)->editable('textarea');
            $grid->created_at('创建时间');

            $grid->filter(function ($filter) {

                // Sets the range query for the created_at field
                $filter->like('name', '学校名称');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(School::class, function (Form $form) {
            $directors = User::where('valid', 0)->get(['id', 'name'])
                ->mapWithKeys(function ($item) {
                    return [$item['id'] => $item['name']];
                })
                ->all();
            $form->display('id', 'ID');
            $form->text('name', ' 名称');
            $form->select('user_id', ' 绑定用户')->options($directors);
            $form->text('description', ' 描述');
        });
    }
}
